package ar.unlam.com.cliente;

import java.awt.Font;
import java.awt.TextArea;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.BevelBorder;

import model.Notificacion;
import model.Usuario;

public class PantallaChat {
	private JFrame frame;
	private DefaultListModel<Usuario> modelAmigosList;	
	private JList<Usuario> chatAmigosList;
	private JList<Notificacion> chatNotificacionesList;
	private JTabbedPane chatTabbedPane ;
	private PantallaPrincipal pantallaPrincipal;
	private Boolean activa;
	
	/* Listado de conversaciones					*/
	private Map<Usuario,PanelDeChat> conversaciones;

	public PantallaChat(PantallaPrincipal pantallaPrincipal) {
		initialize(pantallaPrincipal);
	}
	
	public Boolean activa(){
		return activa;
	}
	
	public void setActiva(Boolean b){
		this.activa = b;
	}

	private void initialize(PantallaPrincipal pantallaPrincipal2){
		this.pantallaPrincipal = pantallaPrincipal2;
		this.activa = true;
		
		frame = new JFrame();
		frame.setBounds(100, 100, 950, 600);
		frame.setResizable(false);
		
		JPanel chatBuscarPanel = new JPanel();
		chatBuscarPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		
		JPanel chatBarraIzquierdaPanel = new JPanel();
		chatBarraIzquierdaPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(chatBarraIzquierdaPanel, GroupLayout.PREFERRED_SIZE, 190, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(chatBuscarPanel, GroupLayout.DEFAULT_SIZE, 652, Short.MAX_VALUE)
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(chatBuscarPanel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 472, Short.MAX_VALUE)
						.addComponent(chatBarraIzquierdaPanel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 472, Short.MAX_VALUE))
					.addContainerGap())
		);
		
		JLabel notificacionesNewLabel = new JLabel("Notificaciones");		
		JLabel chatAmigosNewLabel = new JLabel("Amigos");
		
		modelAmigosList = new DefaultListModel<Usuario>();
		chatAmigosList = new JList<Usuario>(modelAmigosList);
		
		Enumeration<Usuario> e = this.pantallaPrincipal.getModelListaAmigos().elements();
		while(e.hasMoreElements())
			modelAmigosList.addElement(e.nextElement());
		
		if(pantallaPrincipal.getListaNotificaciones() != null)
			chatNotificacionesList = pantallaPrincipal.getListaNotificaciones();
		else 
			chatNotificacionesList = new JList<Notificacion>();
		
        JScrollPane scrollAmigos = new JScrollPane(chatAmigosList);
        JScrollPane scrollNotificaciones = new JScrollPane(chatNotificacionesList);
        
		GroupLayout gl_chatBarraIzquierdaPanel = new GroupLayout(chatBarraIzquierdaPanel);
		gl_chatBarraIzquierdaPanel.setHorizontalGroup(
			gl_chatBarraIzquierdaPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_chatBarraIzquierdaPanel.createSequentialGroup()
					.addGroup(gl_chatBarraIzquierdaPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_chatBarraIzquierdaPanel.createSequentialGroup()
							.addGap(19)
							.addComponent(chatAmigosNewLabel))
						.addGroup(gl_chatBarraIzquierdaPanel.createSequentialGroup()
							.addContainerGap()
							.addComponent(notificacionesNewLabel))
						.addGroup(gl_chatBarraIzquierdaPanel.createSequentialGroup()
							.addContainerGap()
							.addComponent(scrollAmigos, GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE))
						.addGroup(gl_chatBarraIzquierdaPanel.createSequentialGroup()
							.addContainerGap()
							.addComponent(scrollNotificaciones, GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)))
					.addContainerGap())
		);
		gl_chatBarraIzquierdaPanel.setVerticalGroup(
			gl_chatBarraIzquierdaPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_chatBarraIzquierdaPanel.createSequentialGroup()
					.addGap(16)
					.addComponent(chatAmigosNewLabel)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(scrollAmigos, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(notificacionesNewLabel)
					.addGap(18)
					.addComponent(scrollNotificaciones, GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
					.addContainerGap())
		);
		chatBarraIzquierdaPanel.setLayout(gl_chatBarraIzquierdaPanel);
		
		chatTabbedPane = new JTabbedPane(JTabbedPane.TOP);
		conversaciones = new HashMap<Usuario,PanelDeChat>();
		
		GroupLayout gl_chatBuscarPanel = new GroupLayout(chatBuscarPanel);
		gl_chatBuscarPanel.setHorizontalGroup(
			gl_chatBuscarPanel.createParallelGroup(Alignment.TRAILING)
				.addComponent(chatTabbedPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 712, Short.MAX_VALUE)
		);
		gl_chatBuscarPanel.setVerticalGroup(
			gl_chatBuscarPanel.createParallelGroup(Alignment.TRAILING)
				.addComponent(chatTabbedPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE)
		);


		chatBuscarPanel.setLayout(gl_chatBuscarPanel);
		frame.getContentPane().setLayout(groupLayout);
		pantallaPrincipal.setVisible(false);
		frame.setVisible(true);
	}	
	
	public void setVisible(boolean b) {
		this.frame.setVisible(b);
	}

	public JFrame getFrame() {
		return frame;
	}

	public void setFrame(JFrame frame) {
		this.frame = frame;
	}

	public JList<Usuario> getChatAmigosList() {
		return chatAmigosList;
	}

	public void setChatAmigosList(JList<Usuario> chatAmigosList) {
		this.chatAmigosList = chatAmigosList;
	}

	public JList<Notificacion> getChatNotificacionesList() {
		return chatNotificacionesList;
	}

	public void setChatNotificacionesList(JList<Notificacion> chatNotificacionesList) {
		this.chatNotificacionesList = chatNotificacionesList;
	}

	public JTabbedPane getChatTabbedPane() {
		return chatTabbedPane;
	}

	public void setChatTabbedPane(JTabbedPane chatTabbedPane) {
		this.chatTabbedPane = chatTabbedPane;
	}

	public Map<Usuario, PanelDeChat> getConversaciones() {
		return conversaciones;
	}

	public void setConversaciones(Map<Usuario, PanelDeChat> conversaciones) {
		this.conversaciones = conversaciones;
	}

	public PantallaPrincipal getPantallaPrincipal() {
		return pantallaPrincipal;
	}

	public void setPantallaPrincipal(PantallaPrincipal pantallaPrincipal) {
		this.pantallaPrincipal = pantallaPrincipal;
	}
}
